package cn.gson.crm.controller.customer;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.CustomerDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.entity.Customer;
import cn.gson.crm.model.entity.Degree;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Status;

/**
 * Servlet implementation class CustomerUpdate
 */
@WebServlet("/cust/update")
public class CustomerUpdate extends BaseServlet {
	private static final long serialVersionUID = 1L;
	CustomerDao cd = new CustomerDao();       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		
		List<Status> listStatus = cd.getAllStatus();
		request.setAttribute("listStatus", listStatus);
		
		List<Degree> listDegree = cd.getAllDegree();
		request.setAttribute("listDegree", listDegree);
		
		List<Employee> emp = new EmpDao().getEmpList();
		request.setAttribute("emp", emp);
		
		forward("customer/customer_manager.jsp");
	}

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String address = request.getParameter("address");
		String tel = request.getParameter("tel");
		String qq = request.getParameter("qq");
		String degreeId = request.getParameter("degreeId");
		String pname = request.getParameter("pname");
		String ptel = request.getParameter("ptel");
		String hobby = request.getParameter("hobby");
		String source = request.getParameter("source");
		
		Customer c = new Customer();
		c.setCust_id(Integer.parseInt(id));
		c.setCust_name(name);
		c.setCust_gender(gender);
		c.setCust_address(address);
		c.setCust_telphone(tel);
		c.setCust_qq(qq);
		c.setCust_degree_id(Integer.parseInt(degreeId));
		c.setCust_parentName(pname);
		c.setCust_parentTel(ptel);
		c.setCust_hobby(hobby);
		c.setCust_source(source);
		
		new CustomerDao().updateCustomer(c);
		redirect("cust/form");
	}
}
